﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace gui
{
    public partial class mainForm : Form
    {
        private SocketClient socketClient;
        private WCFClient wcfClient;
        private bool stateSocketClient = false;
        private bool stateWCFClient = false;
        private ClientSettings settings;
        private Config dataToConfigFile;
        ClientSettings _settings = new ClientSettings();
        

        public mainForm()
        {
            Application.ApplicationExit += new EventHandler(OnApplicationExit);
            InitializeComponent();
            this.settings = new ClientSettings();
            this.dataToConfigFile = new Config();
            this.dataToConfigFile.GetConfig();
            
            if (this.dataToConfigFile.TypeConnection == this.settings.SocektType)
            {
                this.socketClient = new SocketClient();
                stateSocketClient = true;
            }

            if (this.dataToConfigFile.TypeConnection == this.settings.WCFType)
            {
                this.wcfClient = new WCFClient();
                stateWCFClient = true;
                wcfClient.OnMessage += new WCFClient.MessageEventHandler(wcfClient_OnMessage);
            }
        }

        void wcfClient_OnMessage(string message)
        {
            DataBase db = new DataBase();

            // Соединяемся с базой
            db.OpenConnection();

            XElement element = XElement.Parse(message);
            string date = element.Element(this._settings.MessageDateElement).Value;
            string text = element.Element(this._settings.MessageTextElement).Value;

            //Добавляем запись в базу
            db.CreatData(text, DateTime.Now);

            string msg = "(" + date + ")\n" + text + "\n";

            PrintInputTextBox.AppendText(msg);
        }

        public void OnApplicationExit(object sender, EventArgs e)
        {
            if (this.stateSocketClient)
            {
                this.socketClient.closeConnect();
            }
            if (this.stateWCFClient)
            {
                this.wcfClient.Close();
            }
        }

        private void Form1_Closing(object sender, EventArgs e)
        {
            if (this.stateSocketClient)
            {
                this.socketClient.closeConnect();
            }
            if (this.stateWCFClient)
            {
                this.wcfClient.Close();
            }
        }

        private void ConnectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.stateSocketClient)
            {
                this.socketClient.SocketConnect();
            }
            this.timerReceive.Start();
            if (this.stateWCFClient)
            {
                this.wcfClient.Connect();
            }
        }

        private void DisconectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.stateSocketClient)
            {
                this.socketClient.closeConnect();
            }
            if (this.stateWCFClient)
            {
                this.wcfClient.Close();
            }
            
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
            {
                if (senderBox.SelectionLength > 0)
                {
                    senderBox.SelectionStart = senderBox.SelectionStart + senderBox.SelectionLength;
                }
                senderBox.Paste();
            }
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (senderBox.SelectionLength > 0)
                senderBox.Copy();
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (senderBox.SelectedText != "")
                senderBox.Cut();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (this.stateSocketClient)
            {
                this.socketClient.Send(this.senderBox.Text);
                string login = this.socketClient.GetLogin();
                string message = login + " (" + DateTime.Now + ")\n" + this.senderBox.Text + "\n";
                this.PrintInputTextBox.AppendText(message);
            }
            if (this.stateWCFClient)
            {
                var msg = this.senderBox.Text;
                ThreadStart start = new ThreadStart(delegate()
                {
                    this.wcfClient.SendMessage(msg);
                });
                Thread th = new Thread(start);
                th.Start();
                //string login = client.GetLogin();
                string message = "(" + DateTime.Now + ")\n" + this.senderBox.Text + "\n";
                //this.PrintInputTextBox.AppendText(message);
                this.senderBox.Text = String.Empty;
            }
            this.senderBox.Text = String.Empty;
        }

        private void ConfigToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Settings stn = new Settings();
            stn.ShowDialog();
        }

        private void HistoryMessageToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            History hst = new History();
            hst.ShowDialog();
        }

        private void AboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            About abt = new About();
            abt.ShowDialog();
        }

        private void timerReceive_Tick(object sender, EventArgs e)
        {
            string currentMessage = String.Empty;
            if (this.stateSocketClient)
            {
                 currentMessage = this.socketClient.Receive();
            }
            this.Invoke(new MethodInvoker(delegate()
            {
                if (currentMessage.Length != 0)
                {
                    this.PrintInputTextBox.AppendText(currentMessage);
                }
            }));

            if (currentMessage.Length != 0)
            {
                Console.WriteLine(currentMessage);
            }
            
        }

        private void OptionsToolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void PrintInputTextBox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
